new Vue({
  el: "#app",
  data() {
    return {
      Member_SerialNumber: "",
      iJSONArray_member_info: [],
      Purchase_Name: "",
      Activity_SerialNumber: "",
      dialogFormVisible: false,
      formLabelWidth: "120px",
      Supplier_SerialNumber: "",
      Supplier: [],
      Article_SerialNumber: "",
      iJSONArray_article_info: [],
      iJSONArray_hdmc: [],
      options: [
        {
          value: "3",
          label: "签收",
        },
        {
          value: "2",
          label: "发货",
        },
        {
          value: "0",
          label: "未发货",
        },
      ],
      value: "",
      sjje: "",
      min: "",
      form: {
        Open_ID: "",
        phone: "",
        SQLSerialNumber: "P_business_sales_order_detail2",
        Condition: "",
        ExtensionCondition: "",
        // Supplier_SerialNumber: [],
        Sales_Order_SerialNumber: "",
        Sales_Date: null,
        limit: 20,
        page: 1,
      },
      // rules: {
      //   Supplier_SerialNumber: [
      //     { required: true, message: "请选择供应商", trigger: "change" },
      //   ],
      // },
      Supplier: [],
      tableData: [], //表格数据
      loading: false,
      multipleSelection: [],
      isSupplier: false,
      table: true,
    };
  },
  mounted() {
    this.cgys();
    this.xdr();
    // this.gthd();
    // this.cspName();
    if (getCookie("role")) {
      this.isSupplier = true;
      let roleObj = JSON.parse(getCookie("role"));
      this.form.Supplier_SerialNumber = [roleObj.other];
      console.log(roleObj);
    }
    const timer = setTimeout(() => {
      this.getColor();
      clearTimeout(timer)
    })
    // this.getData();
  },
  computed: {
    Type() {
      return function (str) {
        switch (str) {
          case "0":
            return "待发货";
          case "1":
            return "发货中";
          case "2":
            return "已发货";
          default:
            return "";
        }
      };
    },
  },
  methods: {
    getColor() {
      const elements = document.querySelectorAll('.el-descriptions-item__label');
      // 遍历每个元素
      for (let i = 0; i < elements.length; i++) {
        // 获取元素的innerHTML
        let html = elements[i].innerHTML;
        // 使用正则表达式替换所有的 * 为红色的 *
        html = html.replace(/\*/g, '<span style="color: red;">*</span>');
        // 将修改后的HTML重新赋值给元素
        elements[i].innerHTML = html;
      }
      const elements1 = document.querySelectorAll('.el-form-item__label');
      // 遍历每个元素
      for (let i = 0; i < elements1.length; i++) {
        // 获取元素的innerHTML
        let html = elements1[i].innerHTML;
        // 使用正则表达式替换所有的 * 为红色的 *
        html = html.replace(/\*/g, '<span style="color: red;">*</span>');
        // 将修改后的HTML重新赋值给元素
        elements1[i].innerHTML = html;
      }

    },
    //下单人
    xdr() {
      const _this = this;
      // return
      $.ajax({
        headers: requestHeaders(),
        type: "post",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
        // url: "http://192.168.10.3:8082/generalBusiness/generalQueryByTableName",

        data: {
          Data: "business_member_info", //表名
          Condition: "",
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_member_info = Result;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    cgys() {
      const _this = this;
      $.ajax({
        headers: requestHeaders(),
        type: "POST",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
        data: {
          Data: "business_supplier_info", //表名
          Condition: "Row_Status='0'",
        },
        success: function (Result) {
          console.log(Result);
          _this.Supplier = Result;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    qd() {
      this.dialogFormVisible = false;
      console.log(this.multipleSelection);
      this.multipleSelection[0].New_Row_Status = "00";
      this.multipleSelection[0].Purchase_Name = this.Purchase_Name;
      // console.log(this.multipleSelection);

      const _this = this;
      // return;
      $.ajax({
        headers: {
          AccessToken: getCookie("token"),
        },
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        // url: "https://service.kdddy.cn/hnx/ADD/Purchase",
        // url: "https://service.kdddy.cn/hnx/ADD/Purchase",
        //500
        url: "https://service.kdddy.cn/hnx/ADD/PurchaseByYi",
        type: "POST",
        data: JSON.stringify(this.multipleSelection),
        success: function (Result) {
          console.log(Result);
          if (Result.Status === "success") {
            _this.$message.success("提交成功");
            window.location.href = 'index.html';
          } else {
            _this.$message.error("提交失败，请稍后再试");
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest); //config/config.js 响应预检
        },
        dataType: "json",
      });
    },
    //活动名称查商品
    hdhandleChange(value) {
      console.log(value);
      console.log(this.Supplier_SerialNumber);

      const _this = this;
      $.ajax({
        // headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/getArticle/BySupplierID",
        data: {
          id: this.Supplier_SerialNumber,
          aid: value,
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_article_info = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    //供应商查活动And商品
    gyhandleChange(value) {
      console.log(value, "gyhandleChange");
      console.log(this.Supplier_SerialNumber);
      console.log(this.Article_SerialNumber);
      const _this = this;
      //活动
      $.ajax({
        headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/get/Activity",
        data: {
          sid: this.Supplier_SerialNumber,
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_hdmc = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
      //商品
      $.ajax({
        // headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        url: "https://service.kdddy.cn/hnx/getArticle/BySupplierID",
        data: {
          id: this.Supplier_SerialNumber,
          aid: "",
        },
        success: function (Result) {
          console.log(Result);
          _this.iJSONArray_article_info = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleChange(value) {
      console.log(value, "value");
      const _this = this;

      $.ajax({
        headers: requestHeaders(),
        type: "get",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        // url: "https://service.kdddy.cn/hnx/get/SupplierInfo",
        url: "https://service.kdddy.cn/hnx/getArticle/BySupplierID",
        data: {
          id: value, //表名
          aid: "",
        },
        success: function (Result) {
          console.log(Result);
          _this.Supplier = Result.Data;
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    // cspName() {
    //   const _this = this;
    //   $.ajax({
    //     headers: requestHeaders(),
    //     type: "POST",
    //     // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
    //     // url: "https://service.kdddy.cn/hnx/generalBusiness/generalQueryByTableName",
    //     url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
    //     data: {
    //       Data: "business_article_info", //表名
    //       Condition: "Row_Status='0'",
    //     },
    //     success: function (Result) {
    //       console.log(Result);
    //       _this.iJSONArray_article_info = Result;
    //     },
    //     complete: function (XMLHttpRequest) {
    //       //   requestPretreatment(XMLHttpRequest);
    //     },
    //     dataType: "json",
    //   });
    // },
    // gthd() {
    //   const _this = this;
    //   $.ajax({
    //     headers: requestHeaders(),
    //     type: "POST",
    //     // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
    //     // url: "https://service2.kdddy.cn/hnx/generalBusiness/generalQueryByTableName",
    //     url: "https://service2.essp.top/hnx/generalBusiness/generalQueryByTableName",
    //     // url: "http://192.168.10.3:9082/generalBusiness/generalQueryByTableName",
    //     data: {
    //       Data: "business_activity", //表名
    //       Condition: "Row_Status='2'",
    //     },
    //     success: function (Result) {
    //       console.log(Result);
    //       _this.iJSONArray_hdmc = Result;
    //     },
    //     complete: function (XMLHttpRequest) {
    //       //   requestPretreatment(XMLHttpRequest);
    //     },
    //     dataType: "json",
    //   });
    // },
    // handleChange(row) {
    //   const inputValue = parseFloat(row.Amount);
    //   if (!isNaN(inputValue)) {
    //     row.Amount = inputValue + 3;
    //   } else {
    //     console.error("输入不是有效的数字");
    //     // 可以根据需要添加其他处理逻辑，如重置输入框的值等
    //     // row.Amount = 0;
    //   }
    // },
    chaxun() {
      this.form.Supplier_SerialNumber = this.Supplier_SerialNumber;
      this.form.start = this.form.Sales_Date[0];
      this.form.end = this.form.Sales_Date[1];
      this.form.pid = this.form.Sales_Order_SerialNumber;
      this.form.Article_SerialNumber = this.Article_SerialNumber;
      this.form.status = this.value;
      this.form.Activity_SerialNumber = this.Activity_SerialNumber;
      console.log(this.form);
      let lArray = new Array();
      lArray.push(this.form);
      const _this = this;
      $.ajax({
        headers: {
          AccessToken: getCookie("token"),
        },
        contentType: "application/json", // 设置请求体的媒体类型为application/json
        // headers: requestHeaders(),
        type: "post",
        // url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
        //500
        url: "https://service.kdddy.cn/hnx/Get/RequestDetailByJia1",
        data: JSON.stringify(lArray),
        success: function (Result) {
          console.log(Result);
          _this.tableData = Result.Data
          _this.table = false;
          const timer = setTimeout(() => {
            _this.table = true;
            clearTimeout(timer)
          })
        },
        complete: function (XMLHttpRequest) {
          //   requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    onSerach(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.form.PaginationNumber = 1;
          this.getList(this.form);
        } else {
          this.$message({
            message: "请选择供应商",
            type: "warning",
          });
          return false;
        }
      });
    },
    handleSelectionChange(val) {
      console.log(val);

      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      this.form.page = 1;
      this.form.limit = val;
    },
    handleCurrentChange(val) {
      this.form.page = val;
    },
    // getData() {
    //   const that = this;
    //   $.ajax({
    //     headers: requestHeaders(),
    //     url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
    //     type: "POST",
    //     data: {
    //       Data: "business_supplier_info",
    //       Condition: "Row_Status <> '1'",
    //     },
    //     success: function (res) {
    //       that.Supplier = res;
    //     },
    //     error: function () {
    //       that.$message.error("操作失败，请稍后再试");
    //     },
    //     complete: function (XMLHttpRequest) {
    //       that.loading = false;
    //       requestPretreatment(XMLHttpRequest);
    //     },
    //     dataType: "json",
    //   });
    // },
    submitForm() {
      if (this.multipleSelection.length > 0) {
        this.dialogFormVisible = true;
      } else {
        this.$message({
          message: "请选择要对账的订单",
          type: "warning",
        });
      }
      //   if (this.multipleSelection.length > 0) {
      //     this.$prompt("请输入对账单名称", "提示", {
      //       confirmButtonText: "确定",
      //       cancelButtonText: "取消",
      //       inputPattern: /^(?!\s*$).{1,50}$/,
      //       inputErrorMessage: "不能为空且长度小于等于50",
      //     })
      //       .then(({ value }) => {
      //         let arr = [];
      //         let sql = "";
      //         this.multipleSelection.forEach((item) => {
      //           if (sql) {
      //             sql =
      //               sql +
      //               "or Sales_Order_Detail_SerialNumber='" +
      //               item.Sales_Order_Detail_SerialNumber +
      //               "' \r\n";
      //           } else {
      //             sql =
      //               "Sales_Order_Detail_SerialNumber='" +
      //               item.Sales_Order_Detail_SerialNumber +
      //               "' \r\n";
      //           }
      //           //修改状态
      //           let Reconciliation_Status = item.Reconciliation_Status.charAt(1);
      //           item.Reconciliation_Status = "1" + Reconciliation_Status;
      //           let is = true;
      //           arr.forEach((obj) => {
      //             if (obj.Supplier_SerialNumber == item.Supplier_SerialNumber) {
      //               //obj.Purchase_Amount = accAdd(obj.Purchase_Amount, accMul(item.Delivery_Quantity, item.Purchase_Unit_Price))  原先
      //               obj.Purchase_Amount = accAdd(
      //                 obj.Purchase_Amount,
      //                 item.Purchase_Amount
      //               );
      //               obj.Children.business_purchase_order_detail.push({
      //                 Purchase_Order_Detail_SerialNumber: "",
      //                 Purchase_Order_SerialNumber: "",
      //                 Unit_Price_SerialNumber:
      //                   item.Execute_Unit_Price_SerialNumber,
      //                 Sales_Order_Detail_SerialNumber:
      //                   item.Sales_Order_Detail_SerialNumber,
      //                 Supplier_Article_SerialNumber:
      //                   item.Supplier_Article_SerialNumber,
      //                 Purchase_Unit_Price: item.Purchase_Unit_Price,
      //                 Quantity: item.Delivery_Quantity,
      //                 Receipt_Quantity: 0,
      //                 Not_Receipt_Quantity: 0,
      //                 Shippment_Quantity: 0,
      //                 Inventory_Quantity: 0,
      //                 Purchase_Amount: item.Purchase_Amount,
      //                 Notes: item.Notes,
      //                 Amount: accMul(
      //                   item.Delivery_Quantity,
      //                   item.Purchase_Unit_Price
      //                 ),
      //               });
      //               is = false;
      //             }
      //           });
      //           if (is) {
      //             //新
      //             arr.push({
      //               Purchase_Order_SerialNumber: "",
      //               Supplier_SerialNumber: item.Supplier_SerialNumber,
      //               Purchase_Date: getYMDHMSDate(),
      //               Transaction_Type: "",
      //               Purchase_Amount: item.Purchase_Amount,
      //               Purchase_Name: value ? value : "",
      //               Paid_Amount: 0,
      //               Apply_Amount: 0,
      //               Confirm_Amount: 0,
      //               Row_Status: this.isSupplier ? "99" : "00",
      //               Children: {
      //                 business_purchase_order_detail: [
      //                   {
      //                     Purchase_Order_Detail_SerialNumber: "",
      //                     Purchase_Order_SerialNumber: "",
      //                     Unit_Price_SerialNumber:
      //                       item.Execute_Unit_Price_SerialNumber,
      //                     Sales_Order_Detail_SerialNumber:
      //                       item.Sales_Order_Detail_SerialNumber,
      //                     Supplier_Article_SerialNumber:
      //                       item.Supplier_Article_SerialNumber,
      //                     Purchase_Unit_Price: item.Purchase_Unit_Price,
      //                     Quantity: item.Delivery_Quantity,
      //                     Receipt_Quantity: 0,
      //                     Not_Receipt_Quantity: 0,
      //                     Shippment_Quantity: 0,
      //                     Inventory_Quantity: 0,
      //                     Purchase_Amount: item.Purchase_Amount,
      //                     Notes: item.Notes,
      //                     Amount: accMul(
      //                       item.Delivery_Quantity,
      //                       item.Purchase_Unit_Price
      //                     ),
      //                   },
      //                 ],
      //               },
      //             });
      //           }
      //         });
      //         $.ajax({
      //           headers: requestHeaders(),
      //           type: "POST",
      //           url: `${BASE_URL2}/generalBusiness/generalQueryByTableNameWithOriginal`,
      //           data: {
      //             Data: "business_sales_order_detail",
      //             Condition: sql,
      //           },
      //           success: function (data) {
      //             data.forEach((item) => {
      //               let Reconciliation_Status =
      //                 item.Reconciliation_Status.charAt(1);
      //               item.Reconciliation_Status = "1" + Reconciliation_Status;
      //             });
      //             $.ajax({
      //               headers: requestHeaders(),
      //               url: `${BASE_URL2}/generalBusiness/generalSave`,
      //               type: "POST",
      //               data: {
      //                 Data: JSON.stringify({
      //                   Add: {
      //                     business_purchase_order: arr,
      //                     base_operation_log: [
      //                       generateLog(
      //                         "采购对账",
      //                         "平台端操作",
      //                         "business_purchase_order",
      //                         JSON.stringify({
      //                           Modify: {
      //                             business_sales_order_detail:
      //                               that.multipleSelection,
      //                           },
      //                           Add: { business_purchase_order: arr },
      //                         })
      //                       ),
      //                     ],
      //                   },
      //                   Modify: { business_sales_order_detail: data },
      //                 }),
      //               },
      //               success: function (res) {
      //                 if (res.Status === "success") {
      //                   that.$message.success("提交成功");
      //                   window.history.go(-1);
      //                 } else {
      //                   that.$message.error("提交失败，请稍后再试");
      //                 }
      //               },
      //               complete: function (XMLHttpRequest) {
      //                 that.loading = false;
      //                 requestPretreatment(XMLHttpRequest);
      //               },
      //               dataType: "json",
      //             });
      //           },
      //           complete: function (XMLHttpRequest) {
      //             requestPretreatment(XMLHttpRequest);
      //           },
      //           dataType: "json",
      //         });
      //       })
      //       .catch(() => {});
      //   } else {
      //     that.$message({
      //       message: "当前没有需要对账的订单明细",
      //       type: "warning",
      //     });
      //   }
    },
    getList(form) {
      //换页方法
      const that = this;
      let data = deepClone(form);
      data.Condition = window.btoa(
        unescape(
          encodeURIComponent(
            "%" +
            data.Sales_Order_SerialNumber +
            "%,%" +
            data.Sales_Order_SerialNumber +
            "%"
          )
        )
      );
      let sql = "";
      if (data.Sales_Date) {
        sql =
          sql +
          "AND business_collection_detail.Collection_Date BETWEEN '" +
          data.Sales_Date[0] +
          "' AND '" +
          data.Sales_Date[1] +
          "'\r\n";
      }
      if (data.Supplier_SerialNumber.length > 0) {
        let str = "";
        data.Supplier_SerialNumber.forEach((item) => {
          if (str) {
            //后续
            str =
              str +
              " OR business_sales_order_detail.Supplier_SerialNumber = '" +
              item +
              "'\r\n";
          } else {
            //首次
            str =
              " business_sales_order_detail.Supplier_SerialNumber = '" +
              item +
              "'\r\n";
          }
        });
        // // 修改后的代码
        // if (Array.isArray(data.Supplier_SerialNumber)) {
        //   data.Supplier_SerialNumber.forEach((item) => {
        //     // 处理逻辑
        //   });
        // } else {
        //   console.error("data.Supplier_SerialNumber 不是一个数组");
        //   // 或者根据具体情况添加其他处理逻辑，如初始化 data.Supplier_SerialNumber 为一个数组
        //   // data.Supplier_SerialNumber = [];
        // }
        sql = sql + "AND (" + str + ") \r\n";
      }
      data.ExtensionCondition = window.btoa(unescape(encodeURIComponent(sql)));
      $.ajax({
        headers: requestHeaders(),
        url: `${BASE_URL2}/generalBusiness/generalQueryBySQLSerialNumberWithOriginal`,
        type: "POST",
        data,
        success: function (Result) {
          Result.forEach((item) => {
            item.Purchase_Amount = accMul(
              item.Delivery_Quantity,
              item.Purchase_Unit_Price
            );
            item.Purchase_Amount_Original = accMul(
              item.Delivery_Quantity,
              item.Purchase_Unit_Price
            );
            item.Notes = "";
            item.Notes_Original = "";
          });
          that.tableData = Result;
          that.table = false;
          const timer = setTimeout(() => {
            that.table = true;
            clearTimeout(timer)
          })
          if (Result.length == 0) {
            that.$message({
              message: "未查询到可对账的订单明细",
              type: "warning",
            });
          }
        },
        complete: function (XMLHttpRequest) {
          requestPretreatment(XMLHttpRequest);
        },
        dataType: "json",
      });
    },
    handleRemove(index) {
      this.$confirm("是否要删除此行?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.tableData = this.tableData.filter(
            (i, id) => id != (this.form.page - 1) * this.form.limit + index
          );
        })
        .catch(() => { });
    },
  },
});
